package by.gravity.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.util.SparseArray;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.Polyline;

public class MapUtil {
	public static List<LatLng> decodePoly(String encoded) {

		List<LatLng> poly = new ArrayList<LatLng>();
		if (encoded == null) {
			return poly;
		}
		int index = 0, len = encoded.length();
		int lat = 0, lng = 0;

		while (index < len) {
			int b, shift = 0, result = 0;
			do {
				b = encoded.charAt(index++) - 63;
				result |= (b & 0x1f) << shift;
				shift += 5;
			} while (b >= 0x20);
			int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
			lat += dlat;

			shift = 0;
			result = 0;
			do {
				b = encoded.charAt(index++) - 63;
				result |= (b & 0x1f) << shift;
				shift += 5;
			} while (b >= 0x20);
			int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
			lng += dlng;

			LatLng p = new LatLng(lat / 1E5, lng / 1E5);
			poly.add(p);
		}

		return poly;
	}

	public static void removePolylines(List<Polyline> polylineList) {
		for (Polyline polyline : polylineList) {
			polyline.remove();
		}

		polylineList.clear();
	}

	public static void removeMarkers(List<Marker> markerList) {
		for (Marker marker : markerList) {
			marker.remove();
		}
		markerList.clear();
	}

	public static void removeMarkers(HashMap<Integer, Marker> markers) {
		for (int key : markers.keySet()) {
			markers.get(key).remove();
		}

		markers.clear();
	}
}
